<?php

namespace App\Console\Commands\Chanjet;

use App\Http\Controllers\Chanjet\TplusCommonController;
use App\Models\Chanjet\TplusToken;
use App\Services\Chanjet\ChanjetRequest;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class AutoRefreshToken extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:AutoRefreshToken';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Tplus定时-刷新token';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $token = TplusToken::where('is_valid',0)->select(['org_id','access_token','refresh_token','update_at','expires_in'])->get();
        $common = new TplusCommonController();
        foreach ($token as $item){
            $difftime = time() - $item['update_at'];
            //Redis::setex('test_123',self::getExpireTime(),'aaaaaabbbb');
            if($difftime > (60 * 60 * 24 *5)){
                //清空缓存
                Redis::delete('tplus_account_token:' . $item['org_id']);
                //重新获取缓存
                $common->getTplusToken($item['org_id']);
            }
        }
        Log::info($this->description."执行成功");
        return 0;
    }
}
